import wx
import wx.grid as gridlib

class HugeTable(gridlib.PyGridTableBase):
    def __init__(self):
        gridlib.PyGridTableBase.__init__(self)

        self.odd=gridlib.GridCellAttr()
        self.odd.SetBackgroundColour("sky blue")
        self.even=gridlib.GridCellAttr()
        self.even.SetBackgroundColour("sea green")

    def GetAttr(self, row, col, kind):
        attr = [self.even, self.odd][row % 2]
        attr.IncRef()
        return attr

    # This is all it takes to make a custom data table to plug into a
    # wxGrid.  There are many more methods that can be overridden, but
    # the ones shown below are the required ones.  This table simply
    # provides strings containing the row and column values.

    def GetNumberRows(self):
        return 10000

    def GetNumberCols(self):
        return 10000

    def IsEmptyCell(self, row, col):
        return False

    def GetValue(self, row, col):
        return str( (row, col) )

    def SetValue(self, row, col, value):
        self.log.write('SetValue(%d, %d, "%s") ignored.\n' % (row, col, value))


class ProteinTable(HugeTable):
    def __init__(self, data):
        HugeTable.__init__(self)
        self.col_label = ['id', 'start', 'end', 'strand', 'chromosome', 'taxon id']
        self.data = data

    def GetNumberRows(self):
        return len(self.data)

    def GetNumberCols(self):
        return 6

    def IsEmptyCell(self, row, col):
        return False

    def GetColLabelValue(self, col):
        #label = ['id', 'start', 'end', 'strand', 'chromosome', 'strand']
        return self.col_label[col]

    def GetValue(self, row, col):
        row_data = self.data[row].split('\t')
        return row_data[col]


class OrthTable(HugeTable):
    def __init__(self, data):
        HugeTable.__init__(self)
        self.col_label = ['id1', 'id2']
        self.data = data

    def GetNumberRows(self):
        return len(self.data)

    def GetNumberCols(self):
        return 2

    def IsEmptyCell(self, row, col):
        return False

    def GetColLabelValue(self, col):
        #label = ['id', 'start', 'end', 'strand', 'chromosome', 'strand']
        return self.col_label[col]

    def GetValue(self, row, col):
        row_data = self.data[row].split('\t')
        return row_data[col]


class ResultTable(HugeTable):
    def __init__(self, data):
        HugeTable.__init__(self)
        self.col_label = ['id1', 'id2', 'p-value', 'method']
        self.data = data

    def GetNumberRows(self):
        return len(self.data)

    def GetNumberCols(self):
        return 4

    def IsEmptyCell(self, row, col):
        return False

    def GetColLabelValue(self, col):
        #label = ['id', 'start', 'end', 'strand', 'chromosome', 'strand']
        return self.col_label[col]

    def GetValue(self, row, col):
        row_data = self.data[row].split('\t')
        return row_data[col]


class ProteinGrid(gridlib.Grid):
    def __init__(self, task, parent=None, id=-1, filename='',):
        gridlib.Grid.__init__(self, parent, id)
        self.update_data(filename)
        self.task = task

        # The second parameter means that the grid is to take ownership of the
        # table and will destroy it when done.  Otherwise you would need to keep
        # a reference to it and call it's Destroy method later.


        #self.Bind(gridlib.EVT_GRID_CELL_RIGHT_CLICK, self.OnRightDown)

    def get_data(self, filename):
        self.data = []
        if filename != '':
            for line in open(filename):
                tmp = line.strip().split()
                self.task.add_taxon(tmp[-1])
                self.data.append(line.strip())
        self.table = ProteinTable(self.data)


    def update_data(self, filename):
        self.protein_file = filename
        self.get_data(self.protein_file)
        self.SetTable(self.table, True)
        self.Update()


    #def OnRightDown(self, event):
    #    print "hello"
    #    print self.GetSelectedRows()

class OrthGrid(gridlib.Grid):
    def __init__(self, parent=None, id=-1, filename=''):
        gridlib.Grid.__init__(self, parent, id)
        self.update_data(filename)

        # The second parameter means that the grid is to take ownership of the
        # table and will destroy it when done.  Otherwise you would need to keep
        # a reference to it and call it's Destroy method later.


        #self.Bind(gridlib.EVT_GRID_CELL_RIGHT_CLICK, self.OnRightDown)

    def get_data(self, filename):
        self.data = []
        if filename != '':
            for line in open(filename):
                self.data.append(line.strip())
        self.table = OrthTable(self.data)


    def update_data(self, filename):
        self.protein_file = filename
        self.get_data(self.protein_file)
        self.SetTable(self.table, True)
        self.Update()


class ResultGrid(gridlib.Grid):
    def __init__(self, task, parent=None, id=-1, filename='',):
        gridlib.Grid.__init__(self, parent, id)
        self.update_data(filename)
        self.task = task

        # The second parameter means that the grid is to take ownership of the
        # table and will destroy it when done.  Otherwise you would need to keep
        # a reference to it and call it's Destroy method later.


        #self.Bind(gridlib.EVT_GRID_CELL_RIGHT_CLICK, self.OnRightDown)

    def get_data(self, filename):
        self.data = []
        if filename != '':
            for line in open(filename):
                tmp = line.strip().split()
                self.data.append(line.strip())
        self.table = ResultTable(self.data)


    def update_data(self, filename):
        self.protein_file = filename
        self.get_data(self.protein_file)
        self.SetTable(self.table, True)
        self.Update()